
function User() {
	var email = "";
	var pwd = "";
	var auth = "";
	var logged = false;
	var activityManager;
	
	/* --- login routine --- */
	this.doLogin = function() {
		jQuery("#loading").show();
		jQuery("#loginForm").hide();
		
		this.auth = this.makeBaseAuth(); 
			
		// communicate with myHojokiApp via child sandbox bridge in order to login
		childExposed.authWithHojoki(this.auth);
	};	


	this.checkLogin = function(status) {		
		if(status == false) { // Login failed
		
			this.logged = false;
			this.activityManager = null;
			jQuery("#loginForm").show();
			jQuery("#loading").hide();
			showError('input[name="email"]');
			showError('input[name="pwd"]');
			
		} else { // Login successful
		
			this.logged = true;
			this.activityManager = new ActivityManager();
			// user wants to be remembered
			if(jQuery('input[name="remember"]').is(':checked')) {
				this.writeEmail();
				this.writePwd();
			} else { // technically, nothing should be in the local store anyway, just call the removal again to be sure
				this.removeEmail();
				this.removePwd();
			}
			
			setTimeout(function() {
				createSysTray();
				jQuery('body').fadeOut(500, function() { mainWindow.visible = false; });
			}, 1000); // enforce delay, otherwise user wouldn't even see the main window

		}		
	}
	
	
	/* --- encode email and password with base64 and return basic auth string --- */
	this.makeBaseAuth = function() {
	    var tok = this.email + ':' + this.pwd;
	    var hash = Base64.encode(tok);
	    return "Basic " + hash;
	};
	
	
	/* --- logout routine --- */
	this.doLogout = function() {
		this.logged = false;
		this.activityManager = null;
		this.removeEmail();
		this.removePwd();
		
		// show main window
		air.NativeApplication.nativeApplication.icon.bitmaps = [];
		mainWindow.alwaysInFront = true;
		mainWindow.visible = true;
		jQuery('#loginForm').show();
		jQuery('#loading').hide();
		jQuery('body').fadeIn(1000);
	}
	
	
	/* --- wrapper functions for file system access --- */
	this.writeEmail = function() { writeToLocalStore("email", this.email); };
	this.writePwd = function() { writeToLocalStore("pwd", this.pwd); };
	this.readEmail = function() { return readFromLocalStore("email"); };
	this.readPwd = function() { return readFromLocalStore("pwd"); };
	this.removeEmail = function() { removeFromLocalStore("email"); };
	this.removePwd = function() { removeFromLocalStore("pwd"); };
	
	
	/* --- use local file system to store user credentials --- */	
	var writeToLocalStore = function(key,value)	{
		data = new air.ByteArray(); 
		data.writeUTFBytes( value ); 
		air.EncryptedLocalStore.setItem( key, data );
	};	
	var readFromLocalStore = function(key) {
		value = air.EncryptedLocalStore.getItem( key );
		if(value != null) return value.readUTFBytes( value.bytesAvailable );
		return "";
	};	
	var removeFromLocalStore = function(key) {
		air.EncryptedLocalStore.removeItem( key );	
	};
	
	/* --- constructor --- */
	this.email = this.readEmail();
	this.pwd = this.readPwd();

}
